package com.liang.service.user;

import com.liang.dao.BaseDao;
import com.liang.dao.user.UserDao;
import com.liang.dao.user.UserDaoImpl;
import com.liang.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao;
    public UserServiceImpl(){
        userDao = new UserDaoImpl();
    }
    @Override
    public User login(String userCode) {
        Connection connection=null;
        User user = null;

        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection, userCode);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        return user;
    }

    @Override
    public Boolean updatePassword(String userPassword, Integer id) {
        Boolean flag = false;
        try {
            Connection connection = BaseDao.getConnection();
            flag = userDao.updatePassword(connection, userPassword, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        return flag;
    }

    @Override
    public List<User> getUserList(String userName, int userRole, int currentPageNo, int pageSize) {
        List<User> list = new ArrayList<>();
        Connection connection = BaseDao.getConnection();
        try {
            list = userDao.getUserList(connection, userName, userRole, currentPageNo, pageSize);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public int getCount(String userName, int userRole) {
        int count = 0;
        Connection connection = BaseDao.getConnection();
        count = userDao.getCount(connection, userName, userRole);

        return count;
    }

    @Override
    public Boolean deleteUser(Integer uid) {
        Boolean flag = false;
        Connection connection = BaseDao.getConnection();
        flag = userDao.deleteUser(connection, uid);

        return flag;
    }

    @Override
    public User getUserInfoForId(Integer uid) {
        User user = null;
        Connection connection = BaseDao.getConnection();
        user = userDao.getUserInfoForId(connection,uid);

        return user;
    }

    @Override
    public Boolean insertUser(String userCode, String userName, String userPassword, Integer gender, Date birthday, String phone, String address, Integer userRole, Integer createdBy, Date creationDate) {
        Boolean flag = false;

        Connection connection = BaseDao.getConnection();
        flag = userDao.insertUser(connection, userCode, userName, userPassword, gender, birthday,
                phone, address, userRole, createdBy, creationDate);

        return flag;
    }

    @Override
    public Boolean modifyUser(Integer id, String userName, Integer gender, Date birthday, String phone, String address, Integer userRole, Integer modifyBy, Date modifyDate) {
        Boolean flag = false;
        Connection connection = BaseDao.getConnection();
        flag = userDao.modifyUser(connection, id, userName, gender, birthday, phone, address, userRole, modifyBy, modifyDate);

        return flag;
    }
}
